/*   Copyright 2012 Julian Aloofi

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include "Field.hpp"

Tacticus::Field::Field(Tacticus::Player player, Tacticus::Piece piece) :
    player(player), piece(piece)
{
    if (player == NOBODY && (piece != EMPTY && piece != IMPASSABLE))
    {
        throw Common::AssumptionException("Player Nobody can't own " +
            piece.str());
    }
    if ((piece == EMPTY || piece == IMPASSABLE) && player !=
        NOBODY)
    {
        throw Common::AssumptionException("Piece " + piece.str() + " must be " +
            "owned by Nobody!");
    }
}

Tacticus::Field::Field(Piece piece) : piece(piece)
{
    if (piece == EMPTY || piece == IMPASSABLE)
    {
        player = NOBODY;
    }
    else
    {
        throw Common::AssumptionException("Need to specify a player when "
            "constructing a Field!");
    }
}

Tacticus::Player Tacticus::Field::getPlayer() const
{
    return player;
}

Tacticus::Piece Tacticus::Field::getPiece() const
{
    return piece;
}

std::string Tacticus::Field::str() const
{
    return player.str() + piece.str();
}

std::pair<bool, bool> Tacticus::Field::attack(const Tacticus::Field &otherField)
const
{
    if(!isValidAttack(otherField))
        throw Common::FightException("Invalid attack");

    const PieceType &our = piece;
    const PieceType &other = otherField.getPiece();

    if (other == EMPTY)
    {
        return std::pair<bool, bool>(true, false);
    }
    if (other == FLAG)
    {
        return std::pair<bool, bool>(true, false);
    }
    if (other == BOMB)
    {
        if (our == MINER)
            return std::pair<bool, bool>(true, false);
        else
            return std::pair<bool, bool>(false, true);
    }
    if (our == SPY && other == MARSHAL)
    {
        return std::pair<bool, bool>(true, false);
    }

    if (our > other)
    {
        return std::pair<bool, bool>(true, false);
    }
    else if (our < other)
    {
        return std::pair<bool, bool>(false, true);
    }
    else
    {
        return std::pair<bool, bool>(false, false);
    }
}

bool Tacticus::Field::isValidAttack(const Tacticus::Field &otherField) const
{
    if (player == NOBODY)
    {
        return false;
    }
    if (player == otherField.getPlayer())
    {
        return false;
    }
    if (piece == FLAG || piece == BOMB)
    {
        return false;
    }
    if (otherField.getPlayer() == NOBODY && otherField.getPiece() != EMPTY)
    {
        return false;
    }

    return true;
}

bool Tacticus::Field::operator==(const Tacticus::Field &rhs) const
{
    return (player == rhs.getPlayer() && piece == rhs.getPiece());
}

bool Tacticus::Field::operator!=(const Tacticus::Field &rhs) const
{
    return !(*this == rhs);
}

bool Tacticus::Field::operator<(const Field &rhs) const
{
    if (player < rhs.getPlayer())
        return true;
    else if (player > rhs.getPlayer())
        return false;
    else
        return (piece < rhs.getPiece());
}
